/**
 * You are given an integer array nums where the iᵗʰ bag contains nums[i] balls.
 * You are also given an integer maxOperations.
 * <p>
 * You can perform the following operation at most maxOperations times:
 * <p>
 * <p>
 * Take any bag of balls and divide it into two new bags with a positive number
 * of balls.
 * <p>
 * <p>
 * <p>
 * For example, a bag of 5 balls can become two new bags of 1 and 4 balls, or two
 * new bags of 2 and 3 balls.
 * <p>
 * <p>
 * <p>
 * <p>
 * Your penalty is the maximum number of balls in a bag. You want to minimize
 * your penalty after the operations.
 * <p>
 * Return the minimum possible penalty after performing the operations.
 * <p>
 * <p>
 * Example 1:
 * <p>
 * <p>
 * Input: nums = [9], maxOperations = 2
 * Output: 3
 * Explanation:
 * - Divide the bag with 9 balls into two bags of sizes 6 and 3. [9] -> [6,3].
 * - Divide the bag with 6 balls into two bags of sizes 3 and 3. [6,3] -> [3,3,3].
 * The bag with the most number of balls has 3 balls, so your penalty is 3 and you
 * should return 3.
 * <p>
 * <p>
 * Example 2:
 * <p>
 * <p>
 * Input: nums = [2,4,8,2], maxOperations = 4
 * Output: 2
 * Explanation:
 * - Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,8,2] -> [2,4,
 * 4,4,2].
 * - Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,4,4,4,2] -> [2,
 * 2,2,4,4,2].
 * - Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,4,4,2] -> [
 * 2,2,2,2,2,4,2].
 * - Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,4,2] ->
 * [2,2,2,2,2,2,2,2].
 * The bag with the most number of balls has 2 balls, so your penalty is 2, and
 * you should return 2.
 * <p>
 * <p>
 * <p>
 * Constraints:
 * <p>
 * <p>
 * 1 <= nums.length <= 10⁵
 * 1 <= maxOperations, nums[i] <= 10⁹
 * <p>
 * <p>
 * Related Topics 数组 二分查找 👍 204 👎 0
 */


package com.xixi.basicAlgroithms.binarySearch;

import java.util.Arrays;

public class ID01760MinimumLimitOfBallsInABag {
    public static void main(String[] args) {
        Solution solution = new ID01760MinimumLimitOfBallsInABag().new Solution();
    }


    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int minimumSize(int[] nums, int maxOperations) {
            //题目可以转换为，在maxOperations操作内，可以将nums中的袋子分成多个袋子，使得单个袋子里面的最大值，不超过y。求最小的y值。
            //将袋子中球分得越平均，y值就越小。
            //满足maxOperations操作的y在一个范围内，那么肯定有一个左边界值res，使得小于res的值，都无法满足maxOperations，大于res的值都可以在maxOperations中分出来。

            //所以左边界最小可能是1，因为正整数。右边界是nums中的最大值（因为可以不分，但是不会增多）

            int left = 1;
            int right = Arrays.stream(nums).max().getAsInt() + 1;

            //求满足 maxOperations的左边界
            int ans = 0;
            while (left < right) { //每次搜索范围是[left, right) 最后会停留在 [left, left), 表示left以前的值都不满足maxOperation
                int mid = (left + right) / 2;
                long op = 0;
                for (int i = 0; i < nums.length; i++) {
                    op += (nums[i] - 1) / mid; //分成最大为mid需要多少次操作， 如果小于等于mid就不用分，就是0
                }

                if (op > maxOperations) { //如果op数大于操作数，说明mid太小了，分不出来那么多
                    left = mid + 1;

                } else if (op <= maxOperations) { //还可以分，还不到 maxOperations
                    right = mid;
                }


            }
            return left;


        }


    }
//leetcode submit region end(Prohibit modification and deletion)


}